using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace qman.DataHelp
{
	/// <summary>
	/// Summary description for DataAccess.
	/// </summary>
	public class DataAccess
	{
		//public static string myConnectionString = "Database=wm;Server=CMServer\\SQLEXPRESS;User Id=sa;Password=inspired";
        //public static string myConnectionString = "Data Source=localhost;Initial Catalog=ops;Integrated Security=True";
        public static string myConnectionString = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;

		public DataAccess()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public static DataSet getDataSet(string sql)
		{
			DataSet dataset = new DataSet();
			try
			{
			SqlConnection conn = new SqlConnection(myConnectionString);
			SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(sql, conn);
			adapter.Fill(dataset);
			}
			catch (Exception)
			{
			
			}
					
			 return dataset;
		}

        public static SqlDataReader getDataReader(string sql)
        {
            SqlConnection conn = new SqlConnection(myConnectionString);
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataReader reader = null;
            try
            {
                conn.Open();
                reader = command.ExecuteReader();
            }

            catch (Exception) { }
            return reader;
        }

		public static bool updateSource(string sql)
		{
			SqlConnection conn = new SqlConnection(myConnectionString);
			SqlCommand command = new SqlCommand(sql,conn);
			conn.Open();

			try
			{
				command.CommandType = CommandType.Text;
				command.ExecuteNonQuery();
				return true;
			}
			catch (Exception)
			{return false;}

			finally
			{
				conn.Close();
			}


		}

        public static DataSet execSP(string sql)
        {
            DataSet dataset = new DataSet();
            try
            {
                SqlConnection conn = new SqlConnection(myConnectionString);
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand sqlc = new SqlCommand(sql, conn);
                sqlc.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand = sqlc; 
                adapter.Fill(dataset);
            }
            catch (Exception)
            {

            }

            return dataset;
        }
	}
}
